home *** CD-ROM | disk | FTP | other *** search
- /*
- * Graphics utility routines
- */
-
- #include "Utilities.h"
-
- // The PushPort and PopPort routines both operate on a stack of GrafPtrs.
- // This rarely becomes more nested than a few levels, but it makes it
- // easier to temporarily swap a port into and out of scope without
- // having to declare a local stack variable. In a more general design,
- // the stack would hold entire GWorlds.
-
- static GrafPtr portStack[MAXPORTSTACK]; // Stack of saved GrafPtrs
- static short sp = 0;
-
- /*
- * Save the current port, and set the given port (if non-NIL)
- */
-
- void PushPort(GrafPtr port)
- {
- if (sp < MAXPORTSTACK) {
- GetPort(&portStack[sp++]);
- if (port) SetPort(port);
- }
- }
-
- /*
- * Restore the last saved port.
- */
-
- void PopPort()
- {
- if (sp > 0)
- SetPort(portStack[--sp]);
- }
-
- /*
- * Convert a rectangle in place from local coordinates to global ones
- * with respect to the given port. If port is NIL, this will be the
- * current port. This code is Mac specific in that it knows that a
- * Rect structure is two Point structs next to each other.
- */
-
- void LocalToGlobalRect(GrafPtr port, Rect *r)
- {
- PushPort(port);
- LocalToGlobal( (Point *)r ); // Top left
- LocalToGlobal( ((Point *)r) + 1 ); // Bottom right
- PopPort();
- }
-
- /*
- * Convert a rectangle in place from global to local coordinates with
- * respect to the given port. If port is NIL, this will be the
- * current port. This code is Mac specific like above.
- */
-
- void GlobalToLocalRect(GrafPtr port, Rect *r)
- {
- PushPort(port);
- GlobalToLocal( (Point *)r ); // Top left
- GlobalToLocal( ((Point *)r) + 1 ); // Bottom right
- PopPort();
- }
-
- /*
- * Deliver a rectangle, ans, that is the centered version of a
- * given rectangle, r, within another given rectangle, inside.
- */
-
- void CenterRect(Rect *r, Rect *inside, Rect *ans)
- {
- short rx,ry,ix,iy;
-
- // Use the difference between the rectangles' centers as an offset
-
- rx = (r->left + r->right) / 2;
- ry = (r->top + r->bottom) / 2;
-
- ix = (inside->right + inside->left) / 2;
- iy = (inside->bottom + inside->top) / 2;
-
- *ans = *r;
- OffsetRect(ans,ix-rx,iy-ry);
- }
-
- /*
- * Delay by (aBit - .5) ticks, on average
- */
-
- void Wait(short aBit)
- {
- long soon = TickCount() + aBit;
-
- while (TickCount() < soon) ;
- }
-
-
-